<?php
namespace app\api\controller;

use app\common\model as M;

/**
 * 金币商城
 */
class GoldsMall extends \app\api\ApiBase
{
    public $model = '\app\common\model\GoldsGoods';

    public static function authList()
    {
        return ['buys'];
    }

    /**
     * 商品列表
     * @Author   FW
     * @DateTime 2024-03-14
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function goods()
    {
        $where = [
            'goods_type' => input('goods_type'),
            'page'       => $this->page,
            'status'     => 1
        ];
        $list = $this->model::getList($where);
        return $this->returnSuccess($list['data']);
    }

    /**
     * 商品类别
     * @Author   FW
     * @DateTime 2024-03-14
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function types()
    {
        $types = $this->model::$goods_type;
        $list = [];
        foreach ( $types as $key => $val ) {
            $list[] = [
                'name'       => $val,
                'goods_type' => $key,
            ];
        }
        return $this->returnSuccess($list);
    }
	
	/**
	 * 商品详情
	 */
    public function details(int $goods_id)
    {
        $goodsInfo = $this->model::where('id',$goods_id)->find();
        if ( empty($goodsInfo) ) return $this->returnFail('商品不存在');

        $goodsInfo->goods_type_display = $this->model::$goods_type[$goodsInfo->goods_type] ?? '未知分类';
        $goodsInfo->pay_type_display = $this->model::$pay_type[$goodsInfo->pay_type] ?? '未知';
        $goodsInfo->albums = json_decode($goodsInfo->albums);
        if ( !empty($goodsInfo->albums) ) {
            $albums = [];
            if ( $goodsInfo->main_video ) {
                $albums[] = videoToAlbum($goodsInfo->main_video);
            }
            foreach ( $goodsInfo->albums as $album ) {
                $albums[] = imageToAlbum($album);
            }
            $goodsInfo->albums = $albums;
        }
        $goodsInfo->details = M\CommonDetail::getDetails('mall',$goods_id);
        return $this->returnSuccess($goodsInfo);
    }

    /**
     * 商品购买
     * @Author   FW
     * @DateTime 2024-03-15
     * @Email    123763712@qq.com
     * @return   [type]           [description]
     */
    public function buy()
    {
        $goodsId   = input('goods_id');
        $addressId = input('address_id');
        $nums      = input('nums');
        $goodsInfo = $this->model::where('id' , $goodsId)->find();

        $this->model::startTrans();
        try {
            $orderData = [
                'orgn_amt'       => 0,
                'disc_amt'       => 0,
                'order_amt'      => 0,
                'golds'          => 0,
                'order_outer_id' => 0,
                'uid'            => $this->uid,
                'spread_uid'     => $this->spread_uid,
            ];

            // 纯金币购买
            if ( $goodsInfo->pay_type == M\GoldsGoods::PAY_GOLDS ) {
                $orderData['golds'] = $goodsInfo->goods_golds;
            }

            // 金币+钱购买
            if ( $goodsInfo->pay_type == M\GoldsGoods::PAY_GOLDS_RMB ) {
                $orderData['golds'] = $goodsInfo->goods_golds;
                $orderData['orgn_amt'] = $goodsInfo->goods_price;
                $orderData['order_amt'] = $goodsInfo->goods_price;
            }

            // 纯钱购买
            if ( $goodsInfo->pay_type == M\GoldsGoods::PAY_RMB ) {
                $orderData['orgn_amt'] = $goodsInfo->goods_price;
                $orderData['order_amt'] = $goodsInfo->goods_price;
            }

            ## 01 生成子订单
            $outer = M\OrderOuterExchange::create([
                'goods_id'   => $goodsInfo->id,
                'address_id' => $addressId,
                'uid'        => $this->uid,
                'nums'       => $nums
            ]);

            ## 01 生成订单
            M\GoldsGoods::create_order($orderData);

            $this->model::commitTrans();
            return $this->returnSuccess($goodsInfo);
        } catch ( \Exception $e ) {
            $this->model::rollbackTrans();
            return $this->returnFail($e->getMessage());
        }

        $this->model::rollbackTrans();
        return $this->returnFail($e->getMessage());
    }

}//Class End